package com.sto.stoapp.http.request;

import java.nio.ByteBuffer;

import com.sto.stoapp.http.BaseMessage;
import com.sto.stoapp.http.FieldLength;

/**
 * gps请求
 * 
 * @author mgf
 * 
 */
public class GpsRequest extends BaseMessage implements FieldLength {

	private static final int PARAM_COUNT = 13;
	// 消息类型
	private String messageType;
	// 网点编号
	private String siteCodeNumber;
	// 工号
	private String jobNumber;
	// 网点名称
	private String siteName;
	// 姓名
	private String jobName;
	// 手机号
	private String phoneNumber;
	// 经度
	private String longitude;
	// 纬度
	private String latitude;
	// 机器唯一码
	private String Unique;
	// 基站信息
	private String cellId;
	private String locationAreaCode;
	private String mobileCountryCode;
	private String mobileNetWorkCode;

	/**
	 * GPS请求实体
	 * 
	 * @param messageType
	 *            消息类型
	 * @param siteCodeNumber
	 *            网点编号
	 * @param siteName
	 *            网点名称
	 * @param jobNumber
	 *            工号
	 * @param jobName
	 *            姓名
	 * @param phoneNumber
	 *            手机号
	 * @param longitude
	 *            经度
	 * @param latitude
	 *            纬度
	 * @param Unique
	 *            机器唯一码
	 */
	public GpsRequest(String messageType, String siteCodeNumber,
			String siteName, String jobNumber, String jobName,
			String phoneNumber, String longitude, String latitude,
			String Unique, String cellId, String locationAreaCode,
			String mobileCountryCode, String mobileNetWorkCode) {
		this.messageType = messageType;
		this.siteCodeNumber = siteCodeNumber;
		this.siteName = siteName;
		this.jobNumber = jobNumber;
		this.jobName = jobName;
		this.phoneNumber = phoneNumber;
		this.longitude = longitude;
		this.latitude = latitude;
		this.Unique = Unique;
		this.cellId = cellId;
		this.locationAreaCode = locationAreaCode;
		this.mobileCountryCode = mobileCountryCode;
		this.mobileNetWorkCode = mobileNetWorkCode;
	}

	public byte[] toByte() {
		int total = MESSAGE_TYPE_LENGTH + SITE_CODE_LENGTH + JOB_NUMBER_LENGTH
				+ MACHINE_NUMBER_LENGTH + siteName.getBytes().length
				+ jobName.getBytes().length + phoneNumber.getBytes().length
				+ longitude.getBytes().length + latitude.getBytes().length
				+ cellId.length() + locationAreaCode.length()
				+ mobileCountryCode.length() + mobileNetWorkCode.length()
				+ PARAM_COUNT;
//		StringBuffer buffer = new StringBuffer();
//
//		buffer.append(plusString("" + total, MESSAGE_BODY_LENGTH) + SEPARATOR);
//
//		buffer.append(plusString("" + messageType, MESSAGE_TYPE_LENGTH)
//				+ SEPARATOR);
//
//		buffer.append(plusString("" + Unique, MACHINE_NUMBER_LENGTH)
//				+ SEPARATOR);
//
//		buffer.append(siteName + SEPARATOR);
//
//		buffer.append(plusString("" + siteCodeNumber, SITE_CODE_LENGTH)
//				+ SEPARATOR);
//
//		buffer.append(jobName + SEPARATOR);
//
//		buffer.append(plusString("" + jobNumber, JOB_NUMBER_LENGTH) + SEPARATOR);
//
//		buffer.append(phoneNumber + SEPARATOR);
//
//		buffer.append(latitude + SEPARATOR);
//
//		buffer.append(cellId + SEPARATOR);
//
//		buffer.append(locationAreaCode + SEPARATOR);
//
//		buffer.append(mobileCountryCode + SEPARATOR);
//
//		buffer.append(mobileNetWorkCode + SEPARATOR);
//
//		return buffer.toString().getBytes();
        ByteBuffer sb = ByteBuffer.allocate(total + MESSAGE_BODY_LENGTH + 1);
        sb.put(plusString("" + total, MESSAGE_BODY_LENGTH).getBytes()).put(
                SEPARATOR.getBytes());
        sb.put(plusString("" + messageType, MESSAGE_TYPE_LENGTH).getBytes())
                .put(SEPARATOR.getBytes());
        sb.put(plusString("" + Unique, MACHINE_NUMBER_LENGTH).getBytes()).put(
                SEPARATOR.getBytes());
        sb.put(siteName.getBytes()).put(SEPARATOR.getBytes());
        sb.put(plusString("" + siteCodeNumber, SITE_CODE_LENGTH).getBytes())
                .put(SEPARATOR.getBytes());
        sb.put(jobName.getBytes()).put(SEPARATOR.getBytes());
        sb.put(plusString("" + jobNumber, JOB_NUMBER_LENGTH).getBytes()).put(
                SEPARATOR.getBytes());
        sb.put(phoneNumber.getBytes()).put(SEPARATOR.getBytes());
        sb.put(latitude.getBytes()).put(SEPARATOR.getBytes());
        sb.put(longitude.getBytes()).put(SEPARATOR.getBytes());
        sb.put(cellId.getBytes()).put(SEPARATOR.getBytes());
        sb.put(locationAreaCode.getBytes()).put(SEPARATOR.getBytes());
        sb.put(mobileCountryCode.getBytes()).put(SEPARATOR.getBytes());
        sb.put(mobileNetWorkCode.getBytes()).put(SEPARATOR.getBytes());
//        sb.put("\r\n".getBytes());
        
        return sb.array();
	}
}
